'use strict';

const Controller = require('egg').Controller;

class DefaultController extends Controller {
  //通知他人初始化 我
  async initInOther() {
    const { ctx, app } = this;
    const {socket, logger,model} = ctx
    let others = await socket.myself.others();
    if(others&&others.length){
      for(let i = 0; i< others.length;i++){
        app.io.of("/glSocket").sockets[others[i].socketid]&&(await app.io.of("/glSocket").sockets[others[i].socketid].emit('otherEnter', socket.myself)) 
      }
    }  
  }
  //移动
  async behavior() {
    const { ctx, app } = this;
    const Op = app.Sequelize.Op;
    const {socket, logger,model} = ctx
    const query = ctx.args[0];
    let updateField = {}
    if(socket.myself.scene ===2) return;       //角色场景状态为2时为电影院模式
    if(Object.keys(query)[0] === 'positionX'){
      updateField.position_x = query.positionX
    }else if(Object.keys(query)[0] === 'positionY'){
      updateField.position_y = query.positionY
    }
    try{
      //更新自身信息
      await  socket.myself.updateOne(updateField)
      //向指定范围内其他人发送自身最新信息
      let others = await socket.myself.others();
      if(others&&others.length){
        for(let i = 0; i< others.length;i++){
          //只向真正在线的指定用户发送信息
          app.io.of("/glSocket").sockets[others[i].socketid]&&(await app.io.of("/glSocket").sockets[others[i].socketid].emit('behavior', socket.myself))
        }
      }
    }catch(err){
      await socket.emit('err',{
        message: '发送失败！',
        err
      })
    }
  }

  //我 场景状态改变
  async changeScene() {
    const { ctx, app, } = this;
    const Op = app.Sequelize.Op;
    const {socket, logger,model} = ctx;
    const nsp = app.io.of("/glSocket");
    const query = ctx.args[0];
    try{
      await socket.myself.updateOne({
        scene: query.scene,
      });  
      let building  = null;
      //start scene === 2 时为进入电影院场景， 需要找出有空位的电影院，并广播电影院所有人，对应电影院里面的人再回应给我
      if(query.scene === 2){
          building  = await ctx.model.Buildings.findOneAndEnter({
            where: {
              type: 100,
              stay_num: {
                [Op.lt]: 10,
              },
            },
            attributes: ['id','building_name','stay_num'],
          })
          console.log('test',JSON.stringify(building))
          if(building){
            socket.join(building.id); //加入指定房间
            nsp.to(building.id).emit('enterRoom', {})            
            console.log(111,nsp)
            
          }else{   //如果查询不到，则所有电影院满人，创建新电影院
            console.log(2)
          }
      }
      //end
      socket.emit('changeSceneDown',{scene: query.scene,myself: socket.myself, building})
    }catch(err){ 
      if(err.parent&&err.parent.errno==22){//22情况为加入的电影院已满，溢出，重新再找电影院加入
        console.log(111,JSON.stringify(err))
      }
      socket.emit('err',err)
    }
  }
  //谈话框
  async dialogBoxMessage() {
    const { ctx, app } = this;
    const Op = app.Sequelize.Op;
    const {socket, logger,model} = ctx
    const query = ctx.args[0];
    
    try{
      //向指定范围内其他人发送 自身说的话
      let others = await socket.myself.others();
      if(others&&others.length){
        for(let i = 0; i< others.length;i++){
           //只向真正在线的指定用户发送信息
          app.io.of("/glSocket").sockets[others[i].socketid]&&(await app.io.of("/glSocket").sockets[others[i].socketid].emit('dialogBoxMessage',{
            roleid: socket.roleid,
            message: query.message
          }))
        }
      }
    }catch(err){
      await socket.emit('err',{
        message: '请求失败！',
        err
      })
    }

  }
  //离开 退出
  async quit() {
    const { ctx, app } = this;
    const {socket, logger,model} = ctx    
    try{
      //向指定范围内其他人发送 自身说的话
        let others = await socket.myself.others();
        if(others&&others.length){
          for(let i = 0; i< others.length;i++){
            //只向真正在线的指定用户发送信息
            app.io.of("/glSocket").sockets[others[i].socketid]&&(await app.io.of("/glSocket").sockets[others[i].socketid].emit('otherQuit',{
              roleid: socket.roleid,
            }))
          }
        }
    }catch(err){
      await socket.emit('err',{
        message: '请求失败！',
        err,
      })
    }

  }
}

module.exports = DefaultController;
